/**
 * @File name:  DateUtil.java 
 * @Create on:  2009-05-05
 * @Author   :  zhys513
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 **/
package com.pzhyd.sc.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 时间工具类
 * 
 */
public class DateUtil {
    /**
     * 获取指定格式的指定时间，null值转化成默认值
     */
    public static String toFormatString(Date date, String format, String defaultString) {
        if (date == null) {
            return defaultString;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setLenient(false);
        return sdf.format(date);
    }

    /**
     * 获取指定格式的指定时间，null值转化成""
     */
    public static String toFormatString(Date date, String format) {
        return toFormatString(date, format, "");
    }

    /**
     * 返回当日00:00:00
     */
    public static Date beginDate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fmt = sdf.format(date);
        SimpleDateFormat beginSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = null;
        try {
            begin = beginSdf.parse(fmt + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return begin;
    }

    /**
     * 返回当日23:59:59
     */
    public static Date endDate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fmt = sdf.format(date);
        SimpleDateFormat beginSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date end = null;
        try {
            end = beginSdf.parse(fmt + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return end;
    }

    /**
     * 返回当年
     */
    public static String getYearOfNow() {
        Calendar calendar;
        String s;
        calendar = Calendar.getInstance();
        s = (new StringBuffer(String.valueOf(calendar.get(1)))).toString();
        return s;
    }

    /**
     * 返回当月
     */
    public static String getMonthOfNow() {
        String month = "";
        String s;
        Calendar calendar = Calendar.getInstance();
        month = (new StringBuffer(String.valueOf(calendar.get(2) + 1))).toString();
        month = month.length() != 1 ? month : "0" + month;
        s = month;
        return s;
    }

    /**
     * 返回当天
     */
    public static String getDayOfNow() {
        String day = "";
        String s;
        Calendar calendar = Calendar.getInstance();
        day = (new StringBuffer(String.valueOf(calendar.get(5)))).toString();
        day = day.length() != 1 ? day : "0" + day;
        s = day;
        return s;
    }

    public static String getDateOfNow(String division) {
        return getYearOfNow() + division + getMonthOfNow() + division + getDayOfNow();
    }

    public static String getTimeOfNow(String division) {
        String hour = "";
        String minute = "";
        String second = "";
        String s;
        Calendar calendar = Calendar.getInstance();
        hour = (new StringBuffer(String.valueOf(calendar.get(11)))).toString();
        minute = (new StringBuffer(String.valueOf(calendar.get(12)))).toString();
        second = (new StringBuffer(String.valueOf(calendar.get(13)))).toString();
        hour = hour.length() != 1 ? hour : "0" + hour;
        minute = minute.length() != 1 ? minute : "0" + minute;
        second = second.length() != 1 ? second : "0" + second;
        s = hour + division + minute + division + second;
        return s;
    }

    public static String formatString(String strData, String strStringFormat, String strDateFormat) {
        if (strData == null) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(strStringFormat);
            String strReturn = formatter.format(toDate(strData, strDateFormat));
            return strReturn;
        }
    }

    @SuppressWarnings("unused")
    private static boolean isYear(String year) {
        return year.length() == 4;
    }

    @SuppressWarnings("unused")
    private static boolean isMonth(String month) {
        if (month.length() != 1 && month.length() != 2)
            return false;
        return Integer.parseInt(month) <= 12 && Integer.parseInt(month) >= 1;
    }

    @SuppressWarnings("unused")
    private static boolean isDay(String day) {
        if (day.length() != 1 && day.length() != 2)
            return false;
        return Integer.parseInt(day) <= 31 && Integer.parseInt(day) >= 1;
    }

    @SuppressWarnings("unused")
    private static boolean isWeek(String week) {
        return Integer.parseInt(week) <= 53 && Integer.parseInt(week) >= 1;
    }

    private static Date toDate(String strData, String strDateFormat) {
        if (strData == null) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(strDateFormat);
            ParsePosition pos = new ParsePosition(0);
            Date dReturn = formatter.parse(strData, pos);
            return dReturn;
        }
    }

    /**
     * 获取当前日期是星期几
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        String weekDay = weekDays[w];
        return weekDay;
    }

    /**
     * 获取当前日期是星期几
     * 
     * @return 当前日期是星期几
     */
    public static String getWeekOfDateNow() {
        Date dt = new Date();
        String weekDay = getWeekOfDate(dt);
        return weekDay;
    }
    
    public static void main(String[] arg){
    	try {
			StdSchedulerFactory  gSchedulerFactory = new StdSchedulerFactory("a");
			Scheduler sched = gSchedulerFactory.getScheduler();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
    }
}
